2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex1_masterTXMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_B I2C - Master transfer multiple bytes
34  *
35  * Description: In this example, the device is configured as an I2C master. It
36  * starts an I2C transaction in active mode but finishes communicating the data
37  * while still in LPM0. After the last byte has been sent it exits LPM0 to
38  * start the transaction over again. This example can be modified to send data
39  * of an arbitrary length by modifying the main loop. Start the corresponding
40  * slave code before running this master code.
41  *
42  *
43  * /|\ /|\
44  * MSP430i2041 10k 10k MSP430i2041
45  * slave | | master
46  * ----------------- | | -----------------
47  * | P1.6/UCB0SCL|<-|----+->|P1.6/UCB0SCL |
48  * | | | | |
49  * | | | | |
50  * | P1.7/UCB0SDA|<-+------>|P1.7/UCB0SDA |
51  * | | | |
52  *
53  * Author: Zack Lalanne
54  ******************************************************************************/
55 
56 #include "driverlib.h"
57 
58 #define SLAVE_ADDRESS 0x48
59 
60 static uint8_t TXData = 1;
61 static uint8_t TXByteCtr;
62 
63 int main(void) {
64 
65  EUSCI_B_I2C_initMasterParam i2cConfig = {
66  EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
67  4096000, // SMCLK = 4.096MHz
68  EUSCI_B_I2C_SET_DATA_RATE_400KBPS, // Desired I2C Clock of 400kHz
69  0, // No byte counter threshold
70  EUSCI_B_I2C_NO_AUTO_STOP // No Autostop
71  };
72 
73  WDT_hold(WDT_BASE);
74 
75  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
76  CS_setupDCO(CS_INTERNAL_RESISTOR);
77 
78  // Setting SMCLK = DC0 / 4 = 4.096 MHz
79  CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_4);
80 
81  // Setting P1.6 and P1.7 as I2C pins
82  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
83  GPIO_PIN6 | GPIO_PIN7,
84  GPIO_PRIMARY_MODULE_FUNCTION);
85 
86  // Setting up I2C communication at 400kHz using SMCLK
87  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &i2cConfig);
88 
89  // Settings slave address
90  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, SLAVE_ADDRESS);
91 
92  // Enable the module for operation
93  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
94 
95  // Enable needed I2C interrupts
96  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0 |
97  EUSCI_B_I2C_NAK_INTERRUPT);
98  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0 |
99  EUSCI_B_I2C_NAK_INTERRUPT);
100 
101  while(1) {
102 
103  TXByteCtr = 4;
104  TXData = 0;
105 
106  // Make sure last transaction is done sending
107  while(EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE) == EUSCI_B_I2C_SENDING_STOP);
108 
109  TXByteCtr--;
110  EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, TXData++);
111 
112  // Go to sleep and wait for LPM exit
113  __bis_SR_register(LPM0_bits | GIE);
114  }
115 }
116 
117 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
118 #pragma vector=USCI_B0_VECTOR
119 __interrupt
120 #elif defined(__GNUC__)
121 __attribute__((interrupt(USCI_B0_VECTOR)))
122 #endif
123 void USCIB0_ISR(void) {
124  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG)) {
125  case USCI_NONE: break;
126  case USCI_I2C_UCALIFG: break;
127  case USCI_I2C_UCNACKIFG:
128  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
129  break;
130  case USCI_I2C_UCSTTIFG: break;
131  case USCI_I2C_UCSTPIFG: break;
132  case USCI_I2C_UCRXIFG3: break;
133  case USCI_I2C_UCTXIFG3: break;
134  case USCI_I2C_UCRXIFG2: break;
135  case USCI_I2C_UCTXIFG2: break;
136  case USCI_I2C_UCRXIFG1: break;
137  case USCI_I2C_UCTXIFG1: break;
138  case USCI_I2C_UCRXIFG0: break;
139  case USCI_I2C_UCTXIFG0:
140  if(TXByteCtr) {
141  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, TXData++);
142  TXByteCtr--;
143  } else {
144  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
145 
146  // Exit LPM to start next transaction
147  __bic_SR_register_on_exit(LPM0_bits);
148  }
149  break;
150  case USCI_I2C_UCBCNTIFG: break;
151  case USCI_I2C_UCCLTOIFG: break;
152  case USCI_I2C_UCBIT9IFG: break;
153  default: break;
154  }
155 }
uint8_t TXData
void USCIB0_ISR(void)
__bic_SR_register_on_exit(LPM3_bits|GIE)